import { fetchMarketDailySales ,weight_trend,hourlyHistogram} from '@/api/home.js'
import * as types from '../../mutation-types'
export default {
  namespaced: true, // 封闭
  state: {
    options: {},
  },
  mutations: {
      // 分时数据
    [types.SET_CHART_DATA](state, options) {
        state.options = options
      },
  },
  getters: {},
  actions: {
    async fetchMarketDailySales(context, payload) {
        const { year, yesterdayMonth, month, day, marketId, yesterdayDay } = payload
        let data = {}
        let yesterdayData = {}
        try {
          const f1 = await fetchMarketDailySales(marketId, year, month, day) // 今天
          // console.log( yesterdayMonth, yesterdayDay);
          const f2 = await fetchMarketDailySales(marketId, year, yesterdayMonth, yesterdayDay) // 昨天
          await Promise.all([f1, f2]).then((result) => {
            yesterdayData = result[1].data
            data = result[0].data
          }).catch((error) => {
            console.log(error)
          })
        } catch (err) {
          console.log('modules-home-index.js-fetchMarketDailySales', err)
        }
        return [data, yesterdayData]
      },
//小时销售额
      async hourlyHistogram(context, payload) {
        const { year, yesterdayMonth, month, day, marketId, yesterdayDay } = payload
        let data = {}
        let opt=[]
        try {
          data = await hourlyHistogram(marketId, year, month, day) 
          let dataname = [];
          let countnum = [];
          let countnummax = 0;
          for (let i = 0; i < data.data.length; i++) {
            dataname.push(data.data[i].Key.substr(11, 8));
            if (data.data[i].Sales >= countnummax) {
              countnummax = data.data[i].Sales;
            }
            countnum.push(data.data[i].Sales);
          }
          opt = {
            color: ["#2c9dfb", "#FF6145"],
            tooltip: {
              trigger: "axis",
              axisPointer: {
                type: "cross",
                crossStyle: {
                  color: "#999",
                },
              },
            },

            legend: {
              data: ["销售额"],
            },
            xAxis: [
              {
                name: "时间",
                type: "category",
                data: dataname,
                axisPointer: {
                  type: "shadow",
                },
              },
            ],
            yAxis: [
              {
                type: "value",
                name: "销售额",
                min: 0,
                max: countnummax,
                // interval: countnummax / 5,
                axisLabel: {
                  formatter: "{value} ",
                },
              },
            ],
            series: [
              {
                name: "销售额",
                type: "bar",
                data: countnum,
              },
            ],
          };

        } catch (err) {
          console.log('小时销售额', err)
        }
        return opt
      },

//销量趋势
      async weight_trend(context, payload) {
        // const { year, yesterdayMonth, month, day, marketId, yesterdayDay } = payload
        let data = {}
        let opt=[]
        try {
          data = await weight_trend(payload) 
          let dataname = data.data.date;
          let countnum = data.data.res;
          let countfirstmax = [];
          let valueMax = 0;
          let seri = [];
          let lege = [];
          //计算最大值和处理数据
          for (let i = 0; i < countnum.length; i++) {
            countfirstmax.push(0);
            for (let j = 0; j < countnum[i].weight_data.length; j++) {
              if (countnum[i].weight_data[j] * 1 >= countfirstmax[i]) {
                countfirstmax[i] = countnum[i].weight_data[j];
              }
            }

            seri.push({
              name: countnum[i].name,
              type: "bar",
              data: countnum[i].weight_data,
            });
            lege.push(countnum[i].name);
          }

          for (let i = 0; i < countfirstmax.length; i++) {
            if (countfirstmax[i] * 1 >= valueMax) {
              valueMax = countfirstmax[i];
            }
          }
          opt = {
            color: [
              "#FF6145",
              "#55bfc0",
              "#5ebe67",
              "#f4cd49",
              "#e05666",
              "#7c4bd9",
              "#e79f3c",
              "#ef8a8b",
              "#00f",
            ],
            tooltip: {
              trigger: "axis",
              axisPointer: {
                type: "cross",
                crossStyle: {
                  color: "#999",
                },
              },
            },
            legend: {
              data: lege,
            },
            xAxis: [
              {
                name: "时间",
                type: "category",
                data: dataname,
                axisPointer: {
                  type: "shadow",
                },
              },
            ],
            yAxis: [
              {
                type: "value",
                name: "销售量",
                min: 0,
                max: valueMax,
                // interval: countnummax / 5,
                axisLabel: {
                  formatter: "{value} ",
                },
              },
            ],
            series: seri,
          };
        } catch (err) {
          console.log('菜品销量走势', err)
        }
        return opt
      },
  }
   
}